package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

public class AddRemoveShipment extends JPanel {

	/**
		 * 
		 */
	private static final long serialVersionUID = -5963220357391577026L;
	private Manager manager;
	// private JFrame main;
	private JTextField supText;
	private JButton reShip, addShip;
	private JTextArea textArea;
	private JScrollPane confirmation;
	private JTextField storeText;
	private JTextField sidText;
	private JTextField dateText;

	public AddRemoveShipment(Manager manager) {
		this.manager = manager;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		addPanel();
		confirmPanel();

	}

	private void addPanel() {
		JPanel adder = new JPanel();
		adder.setLayout(new GridLayout(6, 2));
		adder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

		adder.add(new JLabel("Sid of shipment:"));
		sidText = new JTextField();
		sidText.setPreferredSize(new Dimension(300, 30));
		adder.add(sidText);

		adder.add(new JLabel("Supplier Name:"));
		supText = new JTextField();
		supText.setPreferredSize(new Dimension(300, 30));
		adder.add(supText);

		adder.add(new JLabel("Name of target store:"));
		storeText = new JTextField();
		storeText.setPreferredSize(new Dimension(300, 30));
		adder.add(storeText);
		
		adder.add(new JLabel("Date:"));
		dateText = new JTextField();
		adder.add(dateText);

		adder.add(new JLabel("Click to insert Shipment"));
		addShip = new JButton("add");
		addShip.addActionListener(new addShip());
		adder.add(addShip);

		adder.add(new JLabel("Click to remove Shipment"));
		reShip = new JButton("remove");
		reShip.addActionListener(new reShip());
		adder.add(reShip);

		add(adder);
	}

	private void confirmPanel() {
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED),
				"Confirmation");
		conPanel.setBorder(title);

		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);

		confirmation = new JScrollPane(textArea);
		confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		conPanel.add(confirmation, BorderLayout.CENTER);

		add(conPanel);
	}

	public class addShip implements ActionListener {
		String supName, storeName, date;
		int sid;

		public void actionPerformed(ActionEvent arg0) {
			supName = supText.getText();
			storeName = storeText.getText();
			sid = Integer.valueOf(sidText.getText());
			date = dateText.getText();
			
			try {
				manager.insertShipment(sid, supName, storeName, date);
				textArea.append("******************* Shipment Inserted ******************" + "\n" +
								"\n" + "sid: " + sid +
								"\n" + "Supplier Name: " + supName +
								"\n" + "Store Name: " + storeName +
								"\n" + "date: " + date +
								"\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("Shipment already exists.  Cannot be added!");
				e.printStackTrace();
			}

		}
	}

	public class reShip implements ActionListener {
		int sid;

		public void actionPerformed(ActionEvent arg0) {
			sid = Integer.valueOf(sidText.getText());
			
			try {
				manager.deleteShipment(sid);
				textArea.append("******************* Shipment Deleted ******************" + "\n"
								+ "\n" + "Shipment with sid: " + sid + " has been removed successfully"
								+ "\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("Shipment does not exist.  Cannot be deleted!");
				e.printStackTrace();
			}

		}

	}
}
